import type { AxiosRequestConfig } from 'axios';
import { clearStorage } from '@/utils/cache';
import axiosinstance from './axiosIns';
import { showError, requestErrorHanlde } from './utils';
const request = async <T>(config: AxiosRequestConfig): Promise<T> => {
  try {
    const resp = await axiosinstance.request<ResponseInfo<T>>(config);
    return Promise.resolve(resp.data.data);
  } catch (err: any) {
    const errorInfo = requestErrorHanlde(err);
    if (errorInfo.code === 401) {
      clearStorage();
      window.location.href = '/401';
    }
    showError(errorInfo.message);
    return Promise.reject(errorInfo);
  }
};

export default request;
